package com.kingint.web.admin.base.controller;


import com.kingint.mbg.dto.MemberReturn;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasDepartmentService;
import com.kingint.mbg.bas.entity.BasDepartment;

import java.util.List;


/**
 * @描述：部门表控制类
 * @作者: wxh
 * @日期: 2020-12-21
 */
@RestController
@RequestMapping("/basDepartment")
@Api(value = "BasDepartmentController", description = "部门表")
@CrossOrigin
public class BasDepartmentController {

    @Autowired
    public BasDepartmentService basDepartmentService;


    @ApiOperation(value = "添加部门表")
    @PostMapping("/create")
//    @PreAuthorize("hasAuthority('basDepartment:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasDepartment basDepartment) {
        int i = basDepartmentService.insertBaseDepartmentInfo(basDepartment);
        if (i > 0) {
            return K.ok().data(basDepartment.getId());
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "更新部门表")
    @PutMapping("/update")
//    @PreAuthorize("hasAuthority('basDepartment:update')")
    public Object update(@Validated(Update.class) @RequestBody BasDepartment basDepartment) {
        int i = basDepartmentService.updateDepartment(basDepartment);
        if (i > 0) {
            return K.ok().data(basDepartment.getId());
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "删除部门表")
    @DeleteMapping("/delete/{id}")
//    @PreAuthorize("hasAuthority('basDepartment:delete')")
    public Object delete(@PathVariable("id") Integer id) {
        int i = basDepartmentService.deleteDepartmentInfo(id);
        if (i > 0) {
            return K.ok();
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "根据部门表名称分页获取部门表列表")
    @GetMapping("/list")
//    @PreAuthorize("hasAuthority('basDepartment:list')")
    public Object getList(BasDepartment basDepartment,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<BasDepartment> list = basDepartmentService.getDepartmentList(basDepartment, pageNum, pageSize);
        return K.ok().data(list);
    }

    @ApiOperation(value = "根据编号查询部门表信息")
    @GetMapping("/{id}")
//    @PreAuthorize("hasAuthority('basDepartment:get')")
    public Object getItem(@PathVariable("id") Integer id) {
        BasDepartment basDepartment = basDepartmentService.getById(id);
        return K.ok().data(basDepartment);
    }

    @ApiOperation(value = "查询部门下的人")
    @GetMapping("/getDepartmentPerson")
    public Object getDepartmentPerson() {
        List<BasDepartment> basDepartment = basDepartmentService.getDepartmentPerson();
        return K.ok().data(basDepartment);
    }

    @ApiOperation(value = "查询所有部门信息")
    @GetMapping("/getAllDepartmentList")
    public Object getAllDepartmentList() {
        List<BasDepartment> basDepartment = basDepartmentService.getAllDepartmentList();
        return K.ok().data(basDepartment);
    }

    @ApiOperation(value = "获取某一部门下的人员信息")
    @GetMapping("/getPersonList")
    public Object getPersonList(@RequestParam(value = "departmentId") Integer departmentId){
        List<MemberReturn> onePerson = basDepartmentService.getPersonList(departmentId, 1);
        return K.ok().data(onePerson);
    }

}